unit WebRequesterThread;

interface

uses
  System.Classes, sqlexpr, warcommons, sysutils, syncobjs,
  ClientDataModuleUnit, WebRequesterUnit;

type
  TRequesterThread = class(TThread)
  private
    FWaitEvent: TEvent;
    FRequesterId: String;
    FBrokerAddress: String;

  protected
    procedure Execute; override;
  public
    property RequesterId: String read FRequesterId write FRequesterId;
    property BrokerAddress: String read FBrokerAddress write FBrokerAddress;

    constructor Create(ACreateSuspended: Boolean); overload;
    destructor Destroy; override;
  end;

implementation

// uses WebRequesterUnit;

{ TRequesterThread }
constructor TRequesterThread.Create(ACreateSuspended: Boolean);
begin
  FWaitEvent := TEvent.Create(nil, True, False, '');
  inherited;
end;

destructor TRequesterThread.Destroy;
begin
  FWaitEvent.Free;
  inherited;
end;

procedure TRequesterThread.Execute;
var
  WebRequester: TWebRequester;
  GetURLResult: integer;
  WaitResult: TWaitResult;

begin
  WebRequester := TWebRequester.Create(nil, RequesterId);
  while True do
  begin
    WebRequester.Start;
    case WebRequester.Status of
      WebRequesterQueueEmpty:
        begin
          // Queue is empty. Wait for a while...
          logMessage('Queue is empty. Wait for a while...');
          WaitResult := FWaitEvent.WaitFor(10000);
          if WaitResult = wrError then
            logMessage('Error while waiting for something to do : ' +
              IntToStr(FWaitEvent.LastError));
        end;
      WebRequesterThrottled:
        begin
          // Throttled by Blizzard. Wait for a while...
          logMessage('Throttled by Blizzard. Wait for a while...');
          WaitResult := FWaitEvent.WaitFor(10000);
          if WaitResult = wrError then
            logMessage('Error while throttled : ' +
              IntToStr(FWaitEvent.LastError));
        end;
      WebRequesterDisconnected:
        begin
          // Requester disconnected from Broker. Wait for a while...
          logMessage('Requester disconnected from Broker. Wait for a while...');
          WaitResult := FWaitEvent.WaitFor(10000);
          if WaitResult = wrError then
            logMessage('Error while waiting for reconnect : ' +
              IntToStr(FWaitEvent.LastError));
        end;
    end;
    WebRequester.Stop;
  end;
end;

end.
